import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { Student } from './student.entity';
import { CreateStudentInput } from './student.input';

@Injectable()
export class StudentService {
  constructor(
    @InjectRepository(Student)
    private studentRepository: Repository<Student>,
  ) {}

  createStudent(createStudentInput: CreateStudentInput): Promise<Student> {
    const { firstName, lastName } = createStudentInput;
    const student = this.studentRepository.create({
      id: Date.now().toString(),
      firstName,
      lastName,
    });

    return this.studentRepository.save(student);
  }

  getAllStudent(): Promise<Student[]> {
    return this.studentRepository.find();
  }

  getStudentById(id: string): Promise<Student> {
    return this.studentRepository.findOne({
      where: {
        id,
      },
    });
  }

  getManyStudents(studentIds: string[]): Promise<Student[]> {
    if (Array.isArray(studentIds)) {
      // console.log('input', studentIds);
      return this.studentRepository.find({
        where: {
          id: {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            $in: studentIds,
          },
        },
      });
    }
    return Promise.resolve([]);
  }
}
